<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8">
    <title>品牌系统</title>

    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>

<body>

<div id="app">

    <!--表单 搜索栏-->
    <el-form :inline="true" :model="pages" class="demo-form-inline">

        <el-form-item label="当前状态">
            <el-select v-model="pages.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>


        <el-form-item label="企业名称">
            <el-input v-model="pages.brandName" placeholder="企业名称"></el-input>
        </el-form-item>

        <el-form-item label="品牌名称">
            <el-input v-model="pages.companyName" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="host()">主菜单</el-button>
        </el-form-item>

    </el-form>

    <!--功能按钮-->
    <el-row>
        <el-button type="danger" plain @click="deleteByIds"> 批量删除</el-button>
        <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>
    </el-row>

    <!--添加数据对话框表单-->
    <el-dialog
            title="新增品牌"
            :visible.sync="dialogVisible"
            width="30%">
        <!--        添加表单-->
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           active-value="1"
                           inactive-value="0">
                </el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="addBrand()">提交</el-button>
                <el-button @click="cancel()">取消</el-button>
            </el-form-item>

        </el-form>

    </el-dialog>

    <!--编辑数据对话框表单-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="dialogVisibleEdit"
            width="30%">
        <!--        添加表单-->
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           active-value="1"
                           inactive-value="0">
                </el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="Edit()">提交</el-button>
                <el-button @click="dialogVisibleEdit=false">取消</el-button>
            </el-form-item>

        </el-form>

    </el-dialog>

    <!--数据表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    align="center"
                    label="当前状态">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">

                <template slot-scope="scope">
                    <el-row>
                        <el-button type="primary" size="mini" plain @click="Update(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" plain @click="Delete(scope.row)">删除</el-button>
                    </el-row>
                </template>

            </el-table-column>

        </el-table>

    </template>

    <!--分页工具条  1.修改页码，3.当前页码，4每页条数.,5.布局模式，6.总记录数-->
    <el-pagination
            class="pages"
            @current-change="handleCurrentChange"
            :current-page="pages.currentPage"
            :page-size="pages.pageSize"
            layout="total,  prev, pager, next, jumper"
            :total="pages.totalCount">
    </el-pagination>

</div>

<script src="plugins/vue.js"></script>
<script src="plugins/element-ui/index.js"></script>
<link rel="stylesheet" href="plugins/element-ui/theme-chalk/index.css">
<script src="plugins/axios-0.18.0.js"></script>

<script>
    new Vue({
        el: "#app",
        mounted() {
            this.selectAll();
        },

        methods: {
            //查询所有
            /*selectAll() {
                axios.get("/brands").then((resp) => {
                    this.tableData = resp.data.data;
                });
            },*/

            selectAll() {
                //获取查询条件，拼接查询条件
                param = "?status="+this.pages.status;
                param += "&brandName="+this.pages.brandName;
                param += "&companyName="+this.pages.companyName;


                axios.get("/brands/"+this.pages.currentPage+"/"+this.pages.pageSize+param).then((resp) => {
                    this.pages.pageSize=resp.data.data.size;
                    this.pages.currentPage=resp.data.data.current;
                    this.pages.totalCount=resp.data.data.total;
                    this.tableData = resp.data.data.records;
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //修改当前页码值为选中的页码值
                this.pages.currentPage=currentPage;
                //执行查询
                this.selectAll();
            },

            //条件查询
            onSubmit() {
                this.selectAll();
            },


            //返回主菜单
            host() {
                axios.get("/brands/"+this.pages.currentPage+"/"+this.pages.pageSize).then((resp) => {
                    this.pages.pageSize=resp.data.data.size;
                    this.pages.currentPage=resp.data.data.current;
                    this.pages.totalCount=resp.data.data.total;
                    this.tableData = resp.data.data.records;
                });
            },

            //新增数据
            addBrand() {
                axios.post("/brands", this.brand).then(resp => {
                    if (resp.data.code === 20011) {
                        //添加成功，关闭窗口
                        this.brand = {};
                        this.dialogVisible = false;
                        this.$message.success('添加成功');
                    } else if (resp.data.code === 20010) {
                        this.$message.error('添加失败');
                    } else {
                        this.$message.error(resp.data.msg)
                    }
                }).finally(() => {
                    this.selectAll();
                });
            },

            //删除数据
            Delete(row) {
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {type: "info"}).then(() => {
                    axios.delete("/brands/" + row.id).then((resp) => {
                        if (resp.data.code === 20021) {
                            this.$message.success('删除成功');
                        } else if (resp.data.code === 20020) {
                            this.$message.error('数据同步失败，自动刷新');
                        } else {
                            this.$message.error(resp.data.msg)
                        }
                    }).finally(() => {
                      this.selectAll();
                    });
                }).catch(() => {
                    this.$message.info("取消操作");
                });
            },

            //批量删除
            deleteByIds() {
                //弹出确认提示框
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        let selectionElement = this.multipleSelection[i];
                        this.selectedIds[i] = selectionElement.id;
                    }
                    //2.发送ajax请求
                    axios({
                        method: "post",
                        url: "http://localhost:8080/brand-case/brand/deleteByIds",
                        data: this.selectedIds
                    }).then(resp => {
                        if (resp.data == "success") {
                            //删除成功，关闭窗口
                            this.dialogVisible = false;
                            //重新查询
                            this.selectAll();

                            this.$message({
                                message: '恭喜你，删除成功',
                                type: 'success'
                            });
                        }
                    })
                }).catch(() => {
                    //用户点击按钮
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            //复选框选中后执行方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            //修改前 通过id加载数据 为了数据同步
            Update(row) {
                axios.get("/brands/" + row.id).then((resp) => {
                    if (resp.data.code === 20041 && resp.data.data != null) {
                        this.brand = resp.data.data;
                        this.dialogVisibleEdit = true;
                    } else if (resp.data.code === 20040) {
                        this.$message.error('数据同步失败，自动刷新');
                    } else {
                        this.$message.error(resp.data.msg)
                    }

                }).finally(() => {
                    this.selectAll();
                });
            },

            //修改数据
            Edit() {
                axios.put("/brands", this.brand).then(resp => {
                    if (resp.data.code === 20031) {
                        this.dialogVisibleEdit = false;
                        this.$message.success('编辑成功');
                    } else if (resp.data.code === 20030) {
                        this.$message.error('数据同步失败，自动刷新');
                    } else {
                        this.$message.error(resp.data.msg)
                    }
                }).finally(() => {
                    this.selectAll();
                })
            },

            //取消按钮
            cancel() {
                this.dialogVisible = false;
                this.dialogVisibleEdit = false;
                this.$message.info("当前操作取消");
            },

            //设置隔行变色
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';

            }
        },
        data() {
            return {
                pages:{
                    //每页显示条数
                    pageSize: 5,
                    //总记录数
                    totalCount: 0,
                    //当前页码
                    currentPage: 1,
                    status: '',
                    brandName: '',
                    companyName:''
                },


                //添加数据对话框是否展示
                dialogVisible: false,
                //编辑数据对话框是否展示
                dialogVisibleEdit: false,
                //品牌模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id: "",
                    ordered: "",
                    description: ""
                },
                //被选择的id数组
                selectedIds: [],

                //复选框选中数据集合
                multipleSelection: [],
                //表格数据
                tableData: [{
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }]
            }
        }
    })
</script>


</body>
</html>